#include "pool_head.h"

int init_pool(process_t *list, int num){
    for(int i = 0; i < num; ++i){
        int local_sockfd_son[2];
        socketpair(AF_LOCAL,SOCK_STREAM,0,local_sockfd_son);

        //创建子进程
        pid_t son_pid = fork();
        if(son_pid == 0){
            close(local_sockfd_son[0]);
            //子进程脱离前台进程组
            setpgid(0,0);
            do_work(local_sockfd_son[1]);
        }else{
            list[i].flag = FREE;
            list[i].local_sockfd = local_sockfd_son[0];
            list[i].pid = son_pid;
            close(local_sockfd_son[1]);
        }
    }
    return 0;
}

int send_to_son(process_t *list,int client_fd,int num){
    for(int i = 0; i < num; ++i){
        if(list[i].flag == FREE){
            //当前进程处于空闲状态
            send_message(list[i].local_sockfd,client_fd,0);
            list[i].flag = BUSY;
            break;
        }
    }
    return 0;
}